package com.hualan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.domain.Heartknowledge;
import com.hualan.mapper.HeartknowledgeMapper;
import com.hualan.service.heartKonwledgeService;
import com.hualan.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作者:烟火味的男孩儿
 * 描述:一条寻找bug的道路
 * 时间:2024/9/9
 */
@Service
public class heartKonwledgeServiceImpl implements heartKonwledgeService {
    @Autowired
    private HeartknowledgeMapper mapper;

    @Override
    //查询所有的方法
    public R queryAllHeartKnowledgeList() {
        List<Heartknowledge> list = mapper.queryheartKonwList();
        if (list == null) {
            return R.fail("查询失败，该部分没有数据....");
        }
        return R.success(list);
    }

    //查询所有的方法
    public PageInfo<Heartknowledge> queryAllHeartKnowledgeListByPage(int pageNum,int pageSize) {
        int count = mapper.selectCountHeartKnows();
        //加载分页设置
        PageHelper.startPage(pageNum,pageSize);
        List<Heartknowledge> list = mapper.queryheartKonwList();
        PageInfo<Heartknowledge> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count);
        return pageInfo;
    }

    @Override
    //添加数据的方法
    public R addHeartKnowledge(Heartknowledge heartknowledge) {
        int row = mapper.insert(heartknowledge);
        if (row == 0) {
            return R.fail(0);
        }
        return R.success(row);
    }

    @Override
    //删除数据的方法
    public R deleteHeartKnowledge(int id) {
        long id1=(long) id;
        int row = mapper.deleteByPrimaryKey(id1);
        if (row == 0) {
            return R.fail(0);
        }
        return R.success(1);
    }

    @Override
    //修改的方法
    public R updateHeartKnowledge(Heartknowledge heartknowledge) {
        int row = mapper.updateByPrimaryKey(heartknowledge);
        if (row == 0) {
            return R.fail(0);
        }
        return R.success(row);
    }

    @Override
    //根据名称进行模糊查询的方法数据的方法
    public R queryHeartKnowledgeByName(String name) {
        List<Heartknowledge> list = mapper.queryHeartKnowledgeByName(name);
        if (list == null) {
            return R.fail("");
        }
        return R.success(list);
    }

    @Override
    //动态修改的sql语句
    public R updateHeartKnowledgeById(Heartknowledge heartknowledge) {
        int row = mapper.updateHeartKnowledgeById(heartknowledge);
        if (row == 0) {
            return R.fail(0);
        }
        return R.success(row);
    }

    @Override
    public R selectCountHeartKnows() {
        int count = mapper.selectCountHeartKnows();
        if (count == 0) {
            return R.fail(0);
        }
        return R.success(count);
    }


}
